/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.farmplant.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.acheieve.entity.Acheieve;
import com.jeesite.modules.acheieve.service.AcheieveService;
import com.jeesite.modules.crop.entity.Crop;
import com.jeesite.modules.crop.service.CropService;
import com.jeesite.modules.goodsinfo.service.GoodsinfoService;
import com.jeesite.modules.person.entity.CropInfo;
import com.jeesite.modules.person.entity.FarmInfo;
import com.jeesite.modules.person.entity.PersonFarmmanagement;
import com.jeesite.modules.person.service.PersonFarmmanagementService;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.vegetablefield.entity.Vegetablefield;
import com.jeesite.modules.vegetablefield.service.VegetablefieldService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.farmplant.entity.Farmplant;
import com.jeesite.modules.farmplant.service.FarmplantService;

import java.util.ArrayList;
import java.util.List;

/**
 * js_farmplantController
 * @author 达芬奇
 * @version 2019-10-05
 */
@Controller
@RequestMapping(value = "${adminPath}/farmplant/farmplant")
public class FarmplantController extends BaseController {

	@Autowired
	private FarmplantService farmplantService;

	@Autowired
	private PersonFarmmanagementService personFarmmanagementService;

	@Autowired
	private VegetablefieldService vegetablefieldService;

	@Autowired
	private CropService cropService;

	@Autowired
	private AcheieveService acheieveService;


	@Autowired
	private GoodsinfoService goodsinfoService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Farmplant get(String id, boolean isNewRecord) {
		return farmplantService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("farmplant:farmplant:view")
	@RequestMapping(value = {"list", ""})
	public String list(Farmplant farmplant, Model model) {
		model.addAttribute("farmplant", farmplant);
		return "modules/farmplant/farmplantList";
	}


	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("farmplant:farmplant:view")
	@RequestMapping(value = "/buyformedit")
	public String Buyformedit(Farmplant farmplant, Model model) {
		model.addAttribute("farmplant", farmplant);
		PersonFarmmanagement personFarmmanagement  =new PersonFarmmanagement();
	 	personFarmmanagement.setUid(UserUtils.getUser().getId());
		//Page<PersonFarmmanagement>page=personFarmmanagementService.findPage(personFarmmanagement);
		List<PersonFarmmanagement> list=personFarmmanagementService.findList(personFarmmanagement);
		List<FarmInfo>list1 = new ArrayList<>();
		for(PersonFarmmanagement personFarmmanagement1:list){
			FarmInfo farmInfo = new FarmInfo();
			farmInfo.setId(personFarmmanagement1.getId());
			Vegetablefield vegetablefield = new Vegetablefield();
			vegetablefield.setId(personFarmmanagement1.getFid());
			farmInfo.setFname(vegetablefieldService.get(vegetablefield).getName()+"_"+personFarmmanagement1.getFsize());
			list1.add(farmInfo);
		}
		List<Crop>crops = cropService.findList(new Crop());
	//	List<CropInfo>cropInfos = new ArrayList<>();
		model.addAttribute("farmInfoList",list1);
		model.addAttribute("CropList",crops);
		return "modules/farmplant/farmplantForm";
	}


	/**
	 * 农场采购引导页面
	 */
	@RequiresPermissions("farmplant:farmplant:view")
	@RequestMapping(value = {"/buylist"})
	public String Buylist(Farmplant farmplant, Model model) {
		model.addAttribute("farmplant", farmplant);
		return "modules/farmplant/farmplantList";
	}

	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("farmplant:farmplant:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Farmplant> listData(Farmplant farmplant, HttpServletRequest request, HttpServletResponse response) {
		farmplant.setPage(new Page<>(request, response));
		Page<Farmplant> page = farmplantService.findPage(farmplant);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("farmplant:farmplant:view")
	@RequestMapping(value = "form")
	public String form(Farmplant farmplant, Model model) {
		model.addAttribute("farmplant", farmplant);
		return "modules/farmplant/farmplantForm";
	}

	/**
	 * 保存农作物种植管理
	 */
	@RequiresPermissions("farmplant:farmplant:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Farmplant farmplant) {
		Farmplant farmplant1 = new Farmplant();
		Crop crop = new Crop();
		crop.setId(farmplant.getCropname());
		farmplant1.setCropname(cropService.get(crop).getName());
		farmplant1.setFid(farmplant.getName());
		Vegetablefield vegetablefield = new Vegetablefield();
		PersonFarmmanagement personFarmmanagement =new PersonFarmmanagement();
		personFarmmanagement.setId(farmplant.getName());
		PersonFarmmanagement personFarmmanagement1 = personFarmmanagementService.get(personFarmmanagement);
		vegetablefield.setId(personFarmmanagement1.getFid());
		Vegetablefield vegetablefield1 =vegetablefieldService.get(vegetablefield);
		//List<PersonFarmmanagement> personFarmmanagement1= personFarmmanagementService.findList(personFarmmanagement);
//		double sum =0;
//		for(PersonFarmmanagement personFarmmanagement2:personFarmmanagement1){
//			sum=sum+personFarmmanagement2.getFsize();
//		}
		farmplant1.setName(vegetablefield1.getName()+"_"+personFarmmanagement1.getFsize());
		farmplant1.setCropnum(personFarmmanagementService.get(personFarmmanagement).getFsize());
		farmplantService.save(farmplant1);
		return renderResult(Global.TRUE, text("保存农作物种植管理成功！"));
	}
	
	/**
	 * 删除农作物种植管理
	 */
	@RequiresPermissions("farmplant:farmplant:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Farmplant farmplant) {
		farmplantService.delete(farmplant);
		return renderResult(Global.TRUE, text("删除农作物种植管理成功！"));
	}

	/**
	 * 收割农作物种植管理
	 */
	@RequiresPermissions("farmplant:farmplant:edit")
	@RequestMapping(value = "achieve")
	@ResponseBody
	public String achieve(Farmplant farmplant) {

		Acheieve acheieve = new Acheieve();
		acheieve.setAchievenum(farmplant.getCropnum());
		acheieve.setCropname(farmplant.getCropname());
		acheieve.setState(0);
		acheieveService.save(acheieve);
		farmplantService.delete(farmplant);
		return renderResult(Global.TRUE, text("收割农作物成功！"));
	}
}